package com.xdtech.transportation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseCondition;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.service.FilterTranslatorService;
import com.xdtech.sys.vo.UserItem;
import com.xdtech.transportation.dao.TranOrderDao;
import com.xdtech.transportation.model.TranOrder;
import com.xdtech.transportation.service.TranOrderService;
import com.xdtech.transportation.vo.TranOrderItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-17 11:46:05
 * @since 1.0
 * @see
 */
@Service
public class TranOrderServiceImpl implements TranOrderService {
	@Autowired
	private TranOrderDao tranOrderDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private FilterTranslatorService filterTranslatorService;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param entity
	 */
	public void save(TranOrder entity) {
		tranOrderDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TranOrder> entities) {
		tranOrderDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param entity
	 */
	public void delete(TranOrder entity) {
		tranOrderDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		tranOrderDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param id
	 * @return
	 */
	public TranOrder get(Long id) {
		return tranOrderDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @return
	 */
	public List<TranOrder> getAll() {
		return tranOrderDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> tranOrders = null;
		long rows = 0;
		if (pg!=null) {
			Page<TranOrder> page = tranOrderDao.findPage(pg,"from TranOrder order by createTime desc", values);
			tranOrders = BeanUtils.copyListProperties(
					TranOrderItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TranOrder> tranOrderList = tranOrderDao.find("from TranOrder order by id desc", values);
			tranOrders = BeanUtils.copyListProperties(
					TranOrderItem.class, tranOrderList);
			rows = tranOrders.size();
		}
		results.put("total", rows);
		results.put("rows", tranOrders);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTranOrder(TranOrderItem item) {
		TranOrder tranOrder = null;
		if (item.getId()==null) {
			tranOrder = new TranOrder();
		}else {
			tranOrder = tranOrderDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(tranOrder, item);
		tranOrderDao.save(tranOrder);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param tranOrderId
	 * @return
	 */
	public TranOrderItem loadTranOrderItem(Long tranOrderId) {
		TranOrder tranOrder = tranOrderDao.get(tranOrderId);
		TranOrderItem tranOrderItem = new TranOrderItem();
		BeanUtils.copyProperties(tranOrderItem, tranOrder);
		return tranOrderItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTranOrderInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 11:46:05
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTranOrderInfo(List<Long> tranOrderIds) {
		for (Long id : tranOrderIds) {
			delete(id);
		}
		return true;
	}
	/* (非 Javadoc)
	 * loadPageWithPrivilege
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param pg
	 * @param values
	 * @return
	 * @see com.xdtech.transportation.service.TranOrderService#loadPageWithPrivilege(com.xdtech.web.model.Pagination, java.util.Map)
	 */
	@Override
	public Map<String, Object> loadPageWithPrivilege(Pagination pg,UserItem currentUser, Map<String, String> values)
	{
		Map<String, Object> results = new HashMap<String, Object>();
		List<TranOrderItem> tranOrders = null;
		if (null == values)
		{
			values = new HashMap<String, String>();
		}
		
		if (!currentUser.isSuperuser())
		{
			//非超级权限的人才进行数据过滤
			String resourcePermission = filterTranslatorService.createTranslatorByResourceKey("TRAN_ORDER",currentUser);
			System.out.println(resourcePermission);
			values.put("resourcePermission", resourcePermission);
		}
		
		Page<TranOrder> page = baseDao.findPageByNamedQuery(pg,"tranOrder.findTranOrderWithPermission",values);
		tranOrders = TranOrderItem.createItems(page.getResult());
		results.put("total", page.getTotalItems());
		results.put("rows", tranOrders);
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:25:43
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
